<template>
  <div>
    <p-table
      class="table-list-container"
      row-key="id"
      :columns="columns"
      bordered
      v-bind="$attrs"
      v-on="$listeners"
    >
      <a
        slot="orderNo"
        slot-scope="text, record"
        @click="$emit('number-click', record)"
      >
        {{ text }}
      </a>
    </p-table>
  </div>
</template>

<script>
import { getDicList } from '@/api/public.js';

export default {
  props: {},
  data() {
    return {};
  },
  computed: {
    typeOptions() {
      return this.$store.state.dictionary.dict['agreement_type'] || {};
    },
    stateOptions() {
      return this.$store.state.dictionary.dict['agreement_status'] || {};
    },
    columns() {
      const columns = [
        {
          align: 'center',
          title: '序号',
          width: 60,
          customRender(t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          align: 'center',
          title: '合同编号',
          dataIndex: 'contractNo',
          width: 150,
          sorter: true,
          ellipsis: true,
          scopedSlots: { customRender: 'orderNo' },
        },
        {
          align: 'center',
          title: '合同名称',
          dataIndex: 'contractName',
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '合同类型',
          dataIndex: 'contractType',
          customRender: (text) => this.typeOptions[text],
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '合同状态',
          dataIndex: 'contractStatus',
          customRender: (text) => this.stateOptions[text],
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '客户简称',
          dataIndex: 'customerName',

          sorter: true,
          ellipsis: true,
          width: 100,
        },
        {
          align: 'center',
          title: '销售合同号',
          dataIndex: 'salesContractNo',
          sorter: true,
          ellipsis: true,
          width: 120,
        },
        {
          align: 'center',
          title: '商机编号',
          dataIndex: 'opportunityNo',
          // customRender: (text) => this.appOptions[text],
          sorter: true,
          width: 100,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '商机名称',
          dataIndex: 'opportunityName',
          sorter: true,
          ellipsis: true,
          width: 100,
        },
        {
          align: 'center',
          title: '创建人',
          dataIndex: 'createUserName',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '最后修改人',
          dataIndex: 'updateUserName',
          sorter: true,
          width: 100,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '最后修改时间',
          dataIndex: 'updateTime',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
      ];
      return columns;
    },
  },
  async created() {
    this.nodeOptions = await this.getDicList('plan_node');
    this.stateOptions = await this.getDicList('state');
  },
  methods: {
    async getDicList(code) {
      if (!code) return [];
      const result = await getDicList(code);
      const data = result?.data?.data || [];
      return data.reduce((pre, cur) => {
        pre[cur.value] = cur.label;
        return pre;
      }, {});
    },
  },
};
</script>

<style lang="less" scoped>
.table-list-container {
  margin-top: 20px;
}
</style>
